package com.survey.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

import com.survey.common.core.domain.entity.SysUser;
import com.survey.common.utils.DateUtils;
import com.survey.common.utils.SecurityUtils;
import com.survey.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.survey.system.mapper.SurIssuePresetMapper;
import com.survey.system.domain.SurIssuePreset;
import com.survey.system.service.ISurIssuePresetService;

/**
 * 预设问题Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-25
 */
@Service
public class SurIssuePresetServiceImpl implements ISurIssuePresetService {
    @Autowired
    private SurIssuePresetMapper surIssuePresetMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询预设问题
     *
     * @param preId 预设问题主键
     * @return 预设问题
     */
    @Override
    public SurIssuePreset selectSurIssuePresetByPreId(Long preId) {
        return surIssuePresetMapper.selectSurIssuePresetByPreId(preId);
    }

    /**
     * 查询预设问题列表
     *
     * @param surIssuePreset 预设问题
     * @return 预设问题
     */
    @Override
    public List<SurIssuePreset> selectSurIssuePresetList(SurIssuePreset surIssuePreset) {
        return surIssuePresetMapper.selectSurIssuePresetList(surIssuePreset);
    }

    /**
     * 新增预设问题
     *
     * @param surIssuePreset 预设问题
     * @return 结果
     */
    @Override
    public int insertSurIssuePreset(SurIssuePreset surIssuePreset) {
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        surIssuePreset.setSchoolId(sysUser.getSchoolId());
        return surIssuePresetMapper.insertSurIssuePreset(surIssuePreset);
    }

    /**
     * 修改预设问题
     *
     * @param surIssuePreset 预设问题
     * @return 结果
     */
    @Override
    public int updateSurIssuePreset(SurIssuePreset surIssuePreset) {
        return surIssuePresetMapper.updateSurIssuePreset(surIssuePreset);
    }

    /**
     * 批量删除预设问题
     *
     * @param preIds 需要删除的预设问题主键
     * @return 结果
     */
    @Override
    public int deleteSurIssuePresetByPreIds(Long[] preIds) {
        return surIssuePresetMapper.deleteSurIssuePresetByPreIds(preIds);
    }

    /**
     * 删除预设问题信息
     *
     * @param preId 预设问题主键
     * @return 结果
     */
    @Override
    public int deleteSurIssuePresetByPreId(Long preId) {
        return surIssuePresetMapper.deleteSurIssuePresetByPreId(preId);
    }

    @Override
    public List<SurIssuePreset> filterList(List<SurIssuePreset> list, Long schoolId) {
        List<SurIssuePreset> result = new ArrayList<>();
        for (SurIssuePreset surIssuePreset : list) {
            if (surIssuePreset.getSchoolId() == 0) {
                result.add(surIssuePreset);
            } else if (surIssuePreset.getSchoolId().equals(schoolId)) {
                result.add(surIssuePreset);
            }
        }
        return result;
    }
}
